﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VISS.API.Users;
using NCT.Framework;
using VISS.Web.Frontend;
using System.Collections.Generic;

namespace VISS.Web.Frontend.Modules.User.Profile {

    public enum EMessageViewType {
        All = 1,
        UnRead = 2
    }

    public partial class MessagesPage : System.Web.UI.Page {

        protected EMessageViewType ViewType = EMessageViewType.All;
        protected int TotalMessage;
        protected string Paging = string.Empty;
        protected int CurrentPage;

        protected void Page_Load(object sender, EventArgs e) {
            UserContext userContext = LoginFactory.GetCurrentUser();
            CurrentPage = ConvertUtils.ToInt(Request.QueryString["page"], 1);
            string strViewType = ConvertUtils.ToString(Request.QueryString["viewtype"]);
            if (userContext == null) {
                Response.Redirect("/login?url=" + AppSettings.RootURL + "profile/messages?viewtype=" + strViewType + "&p=" + CurrentPage);
                return;
            }

            IUserMessage _objectMessage = StructureMapFactory.GetInstance<IUserMessage>();
            string queryString;

            switch (strViewType) {
                case "unread":
                    ViewType = EMessageViewType.UnRead;
                    queryString = AppSettings.RootURL + "profile/messages?viewtype=unread";
                    break;
                default:
                    ViewType = EMessageViewType.All;
                    queryString = AppSettings.RootURL + "profile/messages?viewtype=all";                    
                    break;
            }

            if (ViewType == EMessageViewType.All) {
                idMessagesList.MessageResult = _objectMessage.GetAllConversationWithPaging(userContext.UserKey, CurrentPage, AppSettings.PageSizeOfMessage, out TotalMessage);
            }
            else {
                idMessagesList.MessageResult = _objectMessage.GetUnReadConversationWithPaging(userContext.UserKey, CurrentPage, AppSettings.PageSizeOfMessage, out TotalMessage);
            }
            if (idMessagesList.MessageResult.Count == 1) {
                if (CurrentPage > 1) {
                    CurrentPage--;
                }
            }

            Paging pager = new Paging(TotalMessage);
            pager.PerPage = AppSettings.PageSizeOfMessage;
            pager.QueryString = queryString;
            pager.Param = "page";
            pager.DisplayParam = "p";
            pager.ShowMore = false;
            pager.LinkFormatTemplate = "<div class=\"page\">{0}</div>";
            pager.SelectedPageFormatTemplate = "<div class=\"page selected-page\">{0}</div>";

            Paging = pager.BuildPaging();

            // SEO
            IHeadSeo seo = this.Master as IHeadSeo;
            seo.Title = StringResources.SEO.TitleUserMessage + " | " + userContext.FullName + " | " + StringResources.SEO.TitleDefault;
            seo.Description = StringResources.SEO.DescriptionDefault;
            seo.Keywords = StringResources.SEO.KeywordsDefault;
        }
    }
}